// pages/record/record.js
Page({
  data: {
    name:'',
    age:'',
    num:'',
    address:'',
    user:'',
    tel:'',
    datatrue:'',
    first:'1',
    once:'',
    now:'',
    other:'',
    one:'',
    two:'',
    three:'',
    four:'',
    ones:'',
    twos:'',
    threes:'',
    fours:'',
    bloodOne:"",
    bloodTwo:"",
    bloodThree:"",
    bloodFour:"",
    blood:'',
    injection:'',
    oral:'',
    bindTextAreaBlur:'',
    nowother:'',
    others:'',
    value:'2',
    values:'1',
    drugs:'',
    symptoms: [
      {value: '1', name: '多饮'},
      {value: '2', name: '多尿'},
      {value: '3', name: '多食'},
      {value: '4', name: '视力模糊'}
    ],
    items:[
      {value: '2', name: '否'},
      {value: '1', name: '是'}
    ],
    reaction:[
      {value: '2', name: '否'},
      {value: '1', name: '是'}
    ],
    glycopenia:[
      {value: '3', name: '无'},
      {value: '1', name: '偶尔'},
      // {value: '1', name: '偶尔', checked: 'true'},
      {value: '2', name: '经常'}
    ],
    token:'',
    // dialogShow: true,
    // showOneButtonDialog: true,
    // buttons: [{text: '取消'}, {text: '确定'}],
    // oneButton: [{text: '确定'}],
  },
//   openConfirm: function () {
//     console.log(1234)
//     this.setData({
//         dialogShow: true
//     })
//     console.log(this.data.showOneButtonDialog)
// },
// tapDialogButton(e) {
//     this.setData({
//         dialogShow: true,
//         showOneButtonDialog: true
//     })
//     console.log(this.data.showOneButtonDialog)
// },
// tapOneDialogButton(e) {
//   var that=this;
//   console.log(123422)
//   that.setData({
//         showOneButtonDialog: true
//     })
//     console.log(this.data.showOneButtonDialog)
// },
  onLoad: function (options) {
    // this.gorRecord()
    // this.getData()
  },
  // 姓名
  name(e){
    this.setData({
      name:e.detail.value
    })
  },
  // 年龄
  age(e){
    this.setData({
      age:e.detail.value
    })
  },
  // 编号
  num(e){
    this.setData({
      num:e.detail.value
    })
  },
  // 家庭住址
  address(e){
    this.setData({
      address:e.detail.value
    })
  },
  //联系人
  user(e){
    this.setData({
      user:e.detail.value
    })
  },
  //联系人
  tel(e){
    this.setData({
      tel:e.detail.value
    })
    console.log(this.data.tel)
  },
  //确定日期
  datatrue(e){
    this.setData({
      datatrue:e.detail.value
    })
  },
  //首次使用胰岛素日期
  first(e){
    this.setData({
      first:e.detail.value
    })
  },
  //曾用胰岛素日期
  now(e){
    this.setData({
      now:e.detail.value
    })
  },
  //曾用胰岛素日期
  once(e){
    this.setData({
      once:e.detail.value
    })
  },
  //其他并发症
  bindTextAreaBlur(e){
    this.setData({
      bindTextAreaBlur:e.detail.value
    })
  },
  //第一次血压
  one(e){
    this.setData({
      one:e.detail.value
    })
  },
  //第二次血压
  two(e){
    this.setData({
      two:e.detail.value
    })
  },
  //第三次血压
  three(e){
    this.setData({
      three:e.detail.value
    })
  },
  //第四次血压
  four(e){
    this.setData({
      four:e.detail.value
    })
  },
   //第一次体重
   ones(e){
    this.setData({
      ones:e.detail.value
    })
  },
  //第二次体重
  twos(e){
    this.setData({
      twos:e.detail.value
    })
  },
  //第三次体重
  threes(e){
    this.setData({
      threes:e.detail.value
    })
  },
  //第四次体重
  fours(e){
    this.setData({
      fours:e.detail.value
    })
  },
  //血糖1
  bloodOne(e){
    this.setData({
      bloodOne:e.detail.value
    })
  },
  //血糖2
  bloodTwo(e){
    this.setData({
      bloodTwo:e.detail.value
    })
  },
  //血糖3
  bloodThree(e){
    this.setData({
      bloodThree:e.detail.value
    })
  },
  //血糖4
  bloodFour(e){
    this.setData({
      bloodFour:e.detail.value
    })
  },
  //糖化血糖蛋白
  blood(e){
    this.setData({
      blood:e.detail.value
    })
  },
  //注射
  injection(e){
    this.setData({
      injection:e.detail.value
    })
  },
  //口服
  oral(e){
    this.setData({
      oral:e.detail.value
    })
  },
  //日体征其他
  other(e){
    this.setData({
      other:e.detail.value
    })
  },
  //现用药物其他
  nowother(e){
    this.setData({
      nowother:e.detail.value
    })
  },
  //底部其他
  others(e){
    this.setData({
      others:e.detail.value,
      token:wx.getStorageSync("token")
    })
  },
  gorRecord(){
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/isAddRecieves',
      method:'GET',
      data: {
        wxapp_id:10001,
        token:wx.getStorageSync("token")
      },
      success: res => {
        console.log(res.data.msg)
        if(res.data.msg=='success'){
            this.getData()
        }
        else if(res.data.msg=='您已填写过一次档案'){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
    // wx.navigateTo({ url: './record' });
  },
  getData() {
    this.gorRecord()
    if(this.data.name==''){
      wx.showToast({
        title: '请填写姓名！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.age==''){
      wx.showToast({
        title: '请填写年龄！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.num==''){
      wx.showToast({
        title: '请填写编号！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.address==''){
      wx.showToast({
        title: '请填写家庭住址！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.user==''){
      wx.showToast({
        title: '请填写联系人！',
        icon:"none",
        duration: 1500
      })
    }
    // else if(this.data.tel==''){
    //   wx.showToast({
    //     title: '请填写电话！',
    //     icon:"none",
    //     duration: 1500
    //   })
    // }
    else if (this.data.tel == 0) {
      wx.showToast({
        title: '请输入手机号码！',
        icon:"none",
        duration: 1500
      })
    }else if ((!/^1[3456789]\d{9}$/.test(this.data.tel))) {
      wx.showToast({
        title: '请输入正确的手机号！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.datatrue==''){
      wx.showToast({
        title: '请填写确定糖尿病日期！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.values==''){
      wx.showToast({
        title: '请选择是否首次使用胰岛素！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.first==''){
      wx.showToast({
        title: '请填写首次使用胰岛素日期！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.once==''){
      wx.showToast({
        title: '请填写曾用胰岛素名称！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.now==''){
      wx.showToast({
        title: '请填写现用胰岛素名称！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.checkboxChange==''){
      wx.showToast({
        title: '请选择症状（可多选）！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.bindTextAreaBlur==''){
      wx.showToast({
        title: '请填写是否存在并发症！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.one==''){
      wx.showToast({
        title: '请填写第一次血压！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.two==''){
      wx.showToast({
        title: '请填写第二次血压！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.three==''){
      wx.showToast({
        title: '请填写第三次血压！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.four==''){
      wx.showToast({
        title: '请填写第四次血压！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.ones==''){
      wx.showToast({
        title: '请填写第一次体重！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.twos==''){
      wx.showToast({
        title: '请填写第二次体重！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.threes==''){
      wx.showToast({
        title: '请填写第三次体重！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.fours==''){
      wx.showToast({
        title: '请填写第四次体重！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.bloodOne==''){
      wx.showToast({
        title: '请填写空腹血糖值！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.bloodTwo==''){
      wx.showToast({
        title: '请填写餐后血糖值！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.bloodThree==''){
      wx.showToast({
        title: '请填写随机血糖值！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.bloodFour==''){
      wx.showToast({
        title: '请填写耐量血糖值！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.other==''){
      wx.showToast({
        title: '请填写其他日体征！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.blood==''){
      wx.showToast({
        title: '请填写糖化血糖蛋白！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.oral==''){
      wx.showToast({
        title: '请填写现用口服药物！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.injection==''){
      wx.showToast({
        title: '请填写现用注射药物！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.nowother==''){
      wx.showToast({
        title: '请填写现用其他药物！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.drugs==''){
      wx.showToast({
        title: '请选择是否有药物不良反应！',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.value==''){
      wx.showToast({
        title: '请选择是否有低血糖反应',
        icon:"none",
        duration: 1500
      })
    }
    else if(this.data.others==''){
      wx.showToast({
        title: '请填写其他(备注)！',
        icon:"none",
        duration: 1500
      })
    }
    else{
      wx.request({
        url: 'https://baojian.dobest.site/index.php?s=/api/Archives/archivesAdd&wxapp_id=10001&token='+wx.getStorageSync("token"),
        method:'POST',
        data: {
          // wxapp_id:10001,
          // member_id:wx.getStorageSync("user_id"),
          // token:wx.getStorageSync("token"),
          name:this.data.name,
          age:this.data.age,
          number:this.data.num,
          address:this.data.address,
          phone:this.data.tel,
          contacts:this.data.user,
          confirm_ill:this.data.datatrue,
          first_use:this.data.first,
          once_yds_name:this.data.once,
          now_yds_name:this.data.now,
          complication:this.data.bindTextAreaBlur,
          blood_pressure_one:this.data.one,
          blood_pressure_two:this.data.two,
          blood_pressure_three:this.data.three,
          blood_pressure_four:this.data.four,
          weight_one:this.data.ones,
          weight_two:this.data.twos,
          weight_three:this.data.threes,
          weight_four:this.data.fours,
          fasting:this.data.bloodOne,
          after_dinner:this.data.bloodTwo,
          random:this.data.bloodThree,
          tolerance:this.data.bloodFour,
          others:this.data.other,
          other_examinations:this.data.blood,
          oral_administration:this.data.oral,
          injection:this.data.injection,
          other_medicine:this.data.nowother,
          final_other:this.data.others,
          hypoglycemia:this.data.value,
          is_first_use:this.data.values,
          adverse_reactions:this.data.drugs,
          symptom:this.data.checkboxChange
        },
        success: res => {
          console.log(res.data.msg)
          wx.showToast({
            title: '添加成功',
            icon: 'none',
            duration: 2000
          })
        },
        fail: err => {
          console.log(err)
        }
      })
    }
  },
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      checkboxChange:e.detail.value
    })
    const symptoms = this.data.symptoms
    const values = e.detail.value
    for (let i = 0, lenI = symptoms.length; i < lenI; ++i) {
      symptoms[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (symptoms[i].value === values[j]) {
          symptoms[i].checked = true
          break
        }
      }
    }
    this.setData({
      symptoms
    })
  },
  //低血糖反应
  glycopenia(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      value:e.detail.value
    })
    const glycopenia = this.data.glycopenia
    for (let i = 0, len = glycopenia.length; i < len; ++i) {
      glycopenia[i].checked = glycopenia[i].value === e.detail.value
    }
    this.setData({
      glycopenia
    })
  },
  //是否首次使用胰岛素
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      values:e.detail.value
    })
    const items = this.data.items
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
    this.setData({
      items
    })
  },
  //药物不良反应
  radioChanges(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      drugs:e.detail.value
    })
    const reaction = this.data.reaction
    for (let i = 0, len = reaction.length; i < len; ++i) {
      reaction[i].checked = reaction[i].value === e.detail.value
    }
    this.setData({
      reaction
    })
    console.log(this.data.reaction)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})